# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake", "configure_make")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

cc_library(
    name = "clang_tidy_stub",
    srcs = ["clang_tidy_stub.cc"],
)

configure_make(
    name = "gperftools_build",
    args = [
        "-j`nproc`",
        "-l`nproc`",
    ],
    configure_options = [
        "--enable-shared=no",
        "--disable-frame-pointers",
        "--enable-libunwind",
        "--host x86_64-unknown-linux-gnu",
    ],
    lib_source = "@com_github_gperftools_gperftools//:all",
    linkopts = ["-lpthread"],
    out_static_libs = select({
        "//bazel:debug_tcmalloc": ["libtcmalloc_debug.a"],
        "//conditions:default": ["libtcmalloc_and_profiler.a"],
    }),
    targets = [
        "install-libLTLIBRARIES",
        "install-perftoolsincludeHEADERS",
    ],
)

# Workaround for https://github.com/bazelbuild/rules_foreign_cc/issues/227
cc_library(
    name = "gperftools",
    deps = [
        "gperftools_build",
    ],
)

cmake(
    name = "natsc",
    build_args = [
        "--",  # <- Pass remaining options to the native tool.
        "-j`nproc`",
        "-l`nproc`",
    ],
    cache_entries = {
        "BUILD_TESTING": "OFF",
        # This forces nats to link against boringssl instead of openssl.
        # Since some of our services use both GRPC and nats, this lets
        # us have a single SSL library.
        "CMAKE_SYSTEM_PREFIX_PATH": "$EXT_BUILD_DEPS",
        "NATS_BUILD_EXAMPLES": "OFF",
        "NATS_BUILD_LIB_SHARED": "OFF",
        "NATS_BUILD_STREAMING": "OFF",
        # There's some issue here and the hostname verification from the
        # Cert SAN seems to fail.
        "NATS_BUILD_TLS_FORCE_HOST_VERIFY": "OFF",
    },
    lib_name = select({
        "//bazel:debug_build": "libnats_staticd",
        "//conditions:default": "libnats_static",
    }),
    lib_source = "@com_github_nats_io_natsc//:all",
    targets = [
        "nats_static",
    ],
    deps = ["@boringssl//:ssl"],
)

cmake(
    name = "libuv",
    build_args = [
        "--",  # <- Pass remaining options to the native tool.
        "-j`nproc`",
        "-l`nproc`",
    ],
    cache_entries = {
        "BUILDING_UV_SHARED": "OFF",
        "BUILD_TESTING": "OFF",
        "LIBUV_BUILD_TESTS": "OFF",
        "USING_UV_SHARED": "OFF",
    },
    lib_name = "libuv_a",
    lib_source = "@com_github_libuv_libuv//:all",
    targets = [
        "uv_a",
    ],
)

cmake(
    name = "libarchive",
    build_args = [
        "--",  # <- Pass remaining options to the native tool.
        "-j`nproc`",
        "-l`nproc`",
    ],
    cache_entries = {
        "ENABLE_ACL": "OFF",
        "ENABLE_BZip2": "OFF",
        "ENABLE_CAT": "OFF",
        "ENABLE_CAT_SHARED": "OFF",
        "ENABLE_CNG": "OFF",
        "ENABLE_CPIO": "OFF",
        "ENABLE_CPIO_SHARED": "OFF",
        "ENABLE_EXPAT": "OFF",
        "ENABLE_ICONV": "OFF",
        "ENABLE_LIBB2": "OFF",
        "ENABLE_LIBMD": "OFF",
        "ENABLE_LIBXML2": "OFF",
        "ENABLE_LZ4": "OFF",
        "ENABLE_LZO": "OFF",
        "ENABLE_LibGCC": "OFF",
        "ENABLE_OPENSSL": "OFF",
        "ENABLE_PCREPOSIX": "OFF",
        "ENABLE_TAR": "OFF",
        "ENABLE_TAR_SHARED": "OFF",
        "ENABLE_TEST": "OFF",
        "ENABLE_WERROR": "OFF",
        "ENABLE_XATTR": "OFF",
        "ENABLE_ZSTD": "OFF",
    },
    lib_name = "libarchive",
    lib_source = "@com_github_libarchive_libarchive//:all",
)

cc_binary(
    name = "prom_test",
    srcs = ["prom_test.cc"],
    deps = ["@com_github_jupp0r_prometheus_cpp//core"],
)
